package com.phaeton.beemeup.entity;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.shape.IShape;
import org.andengine.input.touch.TouchEvent;

import android.util.Log;

import com.phaeton.beemeup.GameManager;

public class Wasp extends Insect{
	static final int MAX_HEALTH = 300;
	
	private boolean wantFight = false;
	private float aggro = 0;
	
	public Wasp(float pX, float pY, final GameManager pGameManager) {
		super(pX, pY, pGameManager.getActivity().getSceneManager().getTiledTexture("Wasp"), pGameManager);

		speed = 200;
		health = 300;
		
		this.registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				double dis_nearest = Double.MAX_VALUE;
				
				IShape target = null;
				
				if(state == IInsect.IDLE) {
					if(wannaFight()) {
						//Wasp fighting
						if(pGameManager.mBees.size() > 0){
							double bVSc = Math.random();
							//Fight bee
							if(bVSc < 0.1) {
								target = pGameManager.mBees.values().iterator().next();
							
								for(final Bee bee: pGameManager.mBees.values()){
									double distance = Math.sqrt( (bee.getX() - getX()) * (bee.getX() - getX()) + 
											(bee.getY() - getY()) * (bee.getY() - getY()));
									
									if(distance < dis_nearest) {
										dis_nearest = distance;
										target = bee;
									}
								}
								for(final Fight fight: pGameManager.getFights()){
									double distance = Math.sqrt( (fight.getX() - getX()) * (fight.getX() - getX()) + 
											(fight.getY() - getY()) * (fight.getY() - getY()));
									
									if(distance < dis_nearest) {
										dis_nearest = distance;
										target = fight;
									}
								}
							}
							else {
								//Fight comb
								target = pGameManager.mBeeCombs.values().iterator().next();
								for(final BeeComb comb: mGameManager.getBeeCombs().values()) {
									if((comb.getState() & (IComb.OCCUPIED | IComb.RESERVED | IComb.FIGHT)) == 0) {
										double distance = Math.sqrt( (comb.getX() - getX()) * (comb.getX() - getX()) + 
												(comb.getY() - getY()) * (comb.getY() - getY()));
										
										if(distance < dis_nearest) {
											dis_nearest = distance;
											target = comb;
										}
									}
								}
							}
						}						
					}else {
						//Wasp collecting
						if(getResource() < 3) {
							target = pGameManager.mFlowers.values().iterator().next();
							
							for(final Flower flower: pGameManager.mFlowers.values()){
								double distance = Double.MAX_VALUE;
								
								if(flower.getMaterial() > 0 && (flower.getState() & (Flower.OCCUPIED | Flower.RESERVED)) == 0) {
									distance = Math.sqrt( (flower.getX() - getX()) * (flower.getX() - getX()) + 
										(flower.getY() - getY()) * (flower.getY() - getY()));
								}
								
								if(distance < dis_nearest) {
									dis_nearest = distance;
									target = flower;
								}
							}
						}
						else{
							target = pGameManager.mWaspCombs.values().iterator().next();
							
							for(final WaspComb comb: pGameManager.getWaspCombs().values()) {
								if((comb.getState() & (IComb.OCCUPIED | IComb.RESERVED | IComb.SURROUNDED | IComb.FIGHT)) == 0) {
									double distance = Math.sqrt( (comb.getX() - getX()) * (comb.getX() - getX()) + 
											(comb.getY() - getY()) * (comb.getY() - getY()));
									
									if(distance < dis_nearest) {
										dis_nearest = distance;
										target = comb;
									}
								}
							}
						}
					}
					if(target != null){
						setTarget(target);
						fly();
					}			
				}
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
			}
		});
	}
	
	public int getMaterial(){
		return getResource();
	}
	
	public void changeMaterial(int i){
		changeResource(i);
	}
	
	public void setMaterial(int i){
		setResource(i);
	}
	
	public boolean wannaFight() {
		wantFight = Math.random() < aggro;
//		if(wantFight) this.addState(AGGRESSIVE);
//		else this.removeState(AGGRESSIVE);
		return wantFight;
	}
	
	public float getAggro(){
		return aggro;
	}
	
	public void setAggro(float aggro){
		this.aggro = aggro;
	}
	@Override
	public int getMaxHealth(){
		return MAX_HEALTH;
	}
	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY){
		Log.e("Wasp", "touched wasp");
		if(pSceneTouchEvent.isActionUp() && mGameManager.getSelectedBee() != null){
			Bee bee = mGameManager.getBees().get(mGameManager.getSelectedBee());
			bee.setTarget(this);
			bee.fly();
			return true;
		}
		return false;						
	}
}
